/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: settingshandler.cpp 372 2009-05-05 21:34:08Z jpnurmi $
*/

#include "settingshandler.h"
#include <QSettings>
#include <QAction>

SettingsHandler::SettingsHandler(QObject* parent) :
    QObject(parent)
{
    messageActions = createActions(Message);
    highlightActions = createActions(Highlight);
    updateActions(Message);
    updateActions(Highlight);
}

SettingsHandler::~SettingsHandler()
{
    QSettings settings;
    settings.beginGroup("Settings");

    settings.beginGroup("messages");
    settings.setValue("join", testSetting(Message, Joins));
    settings.setValue("part", testSetting(Message, Parts));
    settings.setValue("nick", testSetting(Message, Nicks));
    settings.setValue("mode", testSetting(Message, Modes));
    settings.setValue("kick", testSetting(Message, Kicks));
    settings.setValue("quit", testSetting(Message, Quits));
    settings.setValue("topic", testSetting(Message, Topics));
    settings.endGroup();

    settings.beginGroup("highlights");
    settings.setValue("join", testSetting(Highlight, Joins));
    settings.setValue("part", testSetting(Highlight, Parts));
    settings.setValue("nick", testSetting(Highlight, Nicks));
    settings.setValue("mode", testSetting(Highlight, Modes));
    settings.setValue("kick", testSetting(Highlight, Kicks));
    settings.setValue("quit", testSetting(Highlight, Quits));
    settings.setValue("topic", testSetting(Highlight, Topics));
    settings.endGroup();
}

QAction* SettingsHandler::action(Type type, Setting setting) const
{
    return type == Message ? messageActions.value(setting) : highlightActions.value(setting);
}

bool SettingsHandler::testSetting(Type type, Setting setting) const
{
    bool result = true;
    QHash<Setting, QAction*> actions = (type == Message ? messageActions : highlightActions);
    if (setting == All)
    {
        foreach (QAction* action, actions)
        {
            if (actions.key(action) != All)
                result &= action->isChecked();
        }
    }
    else
    {
        result = actions.value(setting)->isChecked();
    }
    return result;
}

void SettingsHandler::actionToggled()
{
    QAction* action = qobject_cast<QAction*>(sender());
    Q_ASSERT(action);
    Type type = static_cast<Type>(action->data().toInt());
    updateActions(type);
}

void SettingsHandler::updateActions(Type type)
{
    QHash<Setting, QAction*> actions = (type == Message ? messageActions : highlightActions);

    QAction* senderAction = qobject_cast<QAction*>(sender());
    if (senderAction)
    {
        Setting setting = actions.key(senderAction);
        if (setting == All)
        {
            foreach (QAction* action, actions)
            {
                if (action->data().toInt() != All)
                {
                    action->blockSignals(true);
                    action->setChecked(senderAction->isChecked());
                    action->blockSignals(false);
                }
            }
        }
    }
    
    actions[All]->blockSignals(true);
    actions[All]->setChecked(testSetting(type, All));
    actions[All]->blockSignals(false);
}

QHash<SettingsHandler::Setting, QAction*> SettingsHandler::createActions(Type type)
{
    QHash<Setting, QAction*> actions;
    actions[All] = new QAction(tr("All"), this);
    actions[Joins] = new QAction(tr("Join"), this);
    actions[Parts] = new QAction(tr("Part"), this);
    actions[Nicks] = new QAction(tr("Nick"), this);
    actions[Modes] = new QAction(tr("Mode"), this);
    actions[Kicks] = new QAction(tr("Kick"), this);
    actions[Quits] = new QAction(tr("Quit"), this);
    actions[Topics] = new QAction(tr("Topic"), this);

    actions[All]->setCheckable(true);
    actions[Joins]->setCheckable(true);
    actions[Parts]->setCheckable(true);
    actions[Nicks]->setCheckable(true);
    actions[Modes]->setCheckable(true);
    actions[Kicks]->setCheckable(true);
    actions[Quits]->setCheckable(true);
    actions[Topics]->setCheckable(true);

    QSettings settings;
    settings.beginGroup("Settings");

    settings.beginGroup(type == Message ? "messages" : "highlights");
    actions[Joins]->setChecked(settings.value("join", type == Message).toBool());
    actions[Parts]->setChecked(settings.value("part", type == Message).toBool());
    actions[Nicks]->setChecked(settings.value("nick", type == Message).toBool());
    actions[Modes]->setChecked(settings.value("mode", type == Message).toBool());
    actions[Kicks]->setChecked(settings.value("kick", type == Message).toBool());
    actions[Quits]->setChecked(settings.value("quit", type == Message).toBool());
    actions[Topics]->setChecked(settings.value("topic", type == Message).toBool());
    settings.endGroup();

    foreach (QAction* action, actions)
    {
        action->setData(type);
        connect(action, SIGNAL(toggled(bool)), this, SLOT(actionToggled()));
    }
    return actions;
}
